*--------------------------------------------------------------; * Selects a single-stage cluster sample, in which clusters are ; * grouped into strata. ; *--------------------------------------------------------------; %macro cl1strs(noprint,frame=,strata=,cluster=,setup=,npop=,n=, sample=,seed=); %if %length(&setup) = 0 %then %let setup = %str(setup); %if %length(&npop) = 0 %then %let npop = %str(npop); %if %length(&n) = 0 %then %let n = %str(n); %if %length(&strata) = 0 %then %let strata =strata; %if %length(&frame) = 0 %then %let frame = %str(frame); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&cluster)= 0 %then %let cluster = %str(cluster); %if %length(&seed) = 0 %then %let seed = %str(0); proc sort data = &frame; by &strata &cluster; proc sort data = &setup; by &strata; proc means data = &frame noprint; by &strata &cluster; output out = new1_ n = mi_; data new1_(drop = _type_ _freq_); merge new1_ &setup (keep = &strata &npop &n); by &strata; data idnos_( drop = &npop &n ); set new1_; by &strata; retain nprime_ 0 pprime_ 1 rprime_ 0 u_ 0; flag_ = 1; if first.&strata then do; nprime_ = &npop; rprime_ = &npop - &n; u_ = ranuni(&seed); pprime_ = pprime_*rprime_/nprime_; end; if pprime_ > u_ then do; rprime_ = rprime_ - 1; nprime_ = nprime_ - 1; if nprime_ > 0 then do; pprime_ = pprime_*rprime_/nprime_; end; end; else do; output; nprime_ = nprime_ - 1; pprime_ = 1; u_ = ranuni(&seed); if nprime_ > 0 then do; pprime_ = pprime_*rprime_/nprime_; end; end; drop nprime_ pprime_ rprime_ u_ ; data &sample(drop=flag_ mi_); merge &frame idnos_; by &strata &cluster; if flag_ = 1 then output; %if %length(&noprint) = 0 %then %do; proc print data = idnos_; title1 "Clusters Selected"; var &strata &cluster; proc print data = &sample; title1 "Single-Stage Cluster Sample"; title2 "Combined with Stratification"; title3 "Output Data Set = &sample"; %end; run; title; %mend cl1strs;